package cn.xiaobai.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Logger;
import java.sql.Types;

import cn.xiaobai.config.MysqlConfig;

/**
 * @Author yangdaji
 * @Date 2023/12/24
 * @PackageName cn.xiaobai.utils
 * @ClassName DBUtils
 */
public class DBUtils {

    private static Logger log = Logger.getLogger(DBUtils.class.getName());

    private static String url;
    private static String username;
    private static String password;

    static {
        MysqlConfig instance = MysqlConfig.getInstance();
        url = instance.getUrl();
        username = instance.getUsername();
        password = instance.getPassword();
    }

    public static Connection getConn() {
        try {
            return DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            log.warning("conn fail: " + e.getMessage());
            return null;
        }
    }

    public static void close(Connection conn, ResultSet resultSet) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
